package com.project.lk.nurseapp.nurse.doctorapply.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.project.lk.nurseapp.base.BaseApi;
import com.project.lk.nurseapp.base.PostBaseModel;
import com.project.lk.nurseapp.login.model.UserBean;
import com.project.lk.nurseapp.utils.JsonUtils;
import com.project.lk.nurseapp.utils.ToastUtils;
import com.project.lk.nurseapp.utils.UserInfoUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.FormBody;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */

public class DoctorApplyInfoModel extends PostBaseModel {
    public void postLoadJson(String url,String senderId){

        FormBody.Builder builder=new FormBody.Builder();
        Random random = new Random();
        int nextInt = random.nextInt();
        long currentTimeMillis = System.currentTimeMillis();
        builder.add("signature",postSignToken(currentTimeMillis+"",nextInt+""))
                .add("timestamp",currentTimeMillis+"")
                .add("nonce",nextInt+"")
                .add("senderId",senderId);
        getJsonData(url,builder.build());
    }
    @Override
    public List analyzeJson(String json, String url) {
        Log.e("MLOG",url+json);
        try {
            JSONObject jsonObject=new JSONObject(json);
            String msg = jsonObject.getString("msg");
            String info = jsonObject.getString("info");
            if (TextUtils.equals(msg,"success")){
                JSONObject data = jsonObject.getJSONObject("data");
                List<DoctorApplyInfoBean> list=new ArrayList<>();
                DoctorApplyInfoBean bean = (DoctorApplyInfoBean) JsonUtils.getJsonUtils()
                        .toObject(new TypeToken<DoctorApplyInfoBean>() {
                        }.getType(), data.toString());
                list.add(bean);
                return list;
            }else {
                fail();
                ToastUtils.showShortToast(info);//显示提示信息
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            fail();
//       Log.e("MLOG",e.getMessage());
        }
        return null;
    }

    List<DoctorApplyInfoBean> mList=new ArrayList<>();

    public List<DoctorApplyInfoBean> getmList() {
        return mList;
    }

    @Override
    public void postSuccessOnMainThread(List list, String url) {
        if (list!=null){
            mList.clear();
            mList.addAll(list);
            returnSuccess();
        }

    }

    public void returnSuccess(){
        bridge.bridge(BaseApi.doctor_apply_info_success,null);
    }

    public void fail(){
        bridge.bridge(BaseApi.doctor_apply_info_fail,null);
    }

    @Override
    public void postFailedOnMainThread(String url) {
        fail();
    }
}
